package com.tianzeculture.stagebao.activity.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.business.BusinessActivity;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.util.TimeUtil;
import com.tianzeculture.stagebao.util.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/6/4 0004.
 */

public class NoticeAdapter extends BaseAdapter {
    Context context;

    List<NoticeList> noticeLists;
    Handler handler;

    public NoticeAdapter(Context context, List<NoticeList> noticeLists, Handler handler) {
        this.context = context;
        this.noticeLists = noticeLists;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return noticeLists.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_notice, null);
            holder.ivPublish = view.findViewById(R.id.ivPublish);
            holder.tagItems = view.findViewById(R.id.tagItems);

            holder.tvPublisher = view.findViewById(R.id.tvPublisher);
            holder.tvPublishTime = view.findViewById(R.id.tvPublishTime);
            holder.ivDanbao = view.findViewById(R.id.ivDanbao);
            holder.tvCheckPublisher = view.findViewById(R.id.tvCheckPublisher);
            holder.tvJobTitle = view.findViewById(R.id.tvJobTitle);
            holder.tvDingwei = view.findViewById(R.id.tvDingwei);
            holder.tvTime = view.findViewById(R.id.tvTime);
            holder.tvPerson = view.findViewById(R.id.tvPerson);
            holder.tvSex = view.findViewById(R.id.tvSex);
            holder.tvHeight = view.findViewById(R.id.tvHeight);
            holder.tvBaoxiao = view.findViewById(R.id.tvBaoxiao);
            holder.tvFuzhuang = view.findViewById(R.id.tvFuzhuang);
            holder.tvEntered = view.findViewById(R.id.tvEntered);
            holder.tvPrice = view.findViewById(R.id.tvPrice);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        setData(holder, i);
        return view;
    }

//    public void refreshNoticeLists(List<NoticeList> list) {
//        notifyDataSetChanged();
//    }


    private void setData(final ViewHolder holder, final int i) {
        if (handler == null)
            holder.tvCheckPublisher.setVisibility(View.GONE);

        holder.tvCheckPublisher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message msg = new Message();
                msg.what = 6;
                Bundle bundle = new Bundle();
                bundle.putString("userId", noticeLists.get(i).getAddPeopleId());
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        });

        Glide.with(context).load(PICTURE_URL + noticeLists.get(i).getImgurl())
                .placeholder(R.mipmap.icon_head)
                .error(R.mipmap.icon_head)
                .into(holder.ivPublish);
        holder.tvPublisher.setText(noticeLists.get(i).getAddPeople());
        holder.tvPublishTime.setText(noticeLists.get(i).getAddTime());
        holder.tvJobTitle.setText(noticeLists.get(i).getTitle());
        holder.tvTime.setText(
                noticeLists.get(i).getStarttime().substring(0, 10) + " ~ " +
                        noticeLists.get(i).getEndtime().substring(0, 10));
        holder.tvPerson.setText("人数:" + noticeLists.get(i).getPeople() + "人");
        holder.tvDingwei.setText(noticeLists.get(i).getAddress());
        String sex = "";
        if (noticeLists.get(i).getSex().equals("0"))
            sex = "女";
        else if (noticeLists.get(i).getSex().equals("1"))
            sex = "男";
        else
            sex = "不限";
        holder.tvSex.setText("性别:" + sex);
        holder.tvHeight.setText("身高:" + noticeLists.get(i).getHeight());
        if (noticeLists.get(i).getIstravel().equals("1"))
            holder.tvBaoxiao.setVisibility(View.VISIBLE);
        else
            holder.tvBaoxiao.setVisibility(View.GONE);
        if (noticeLists.get(i).getIscostume().equals("1"))
            holder.tvFuzhuang.setVisibility(View.VISIBLE);
        else
            holder.tvFuzhuang.setVisibility(View.GONE);
        holder.tvEntered.setText("已报名 " + noticeLists.get(i).getCntjob() + " 人");
//        if ("0".equals(noticeLists.get(i).getPriceType()))
        holder.tvPrice.setText("¥" + noticeLists.get(i).getUnitPrice() + "/人/" + (noticeLists.get(i).getPriceType() == null ? "小时" : noticeLists.get(i).getPriceType()));
//        else
//            holder.tvPrice.setText("¥" + noticeLists.get(i).getUnitPrice() + "/人/天");


        List<String> strings = new ArrayList<>();
//        if (noticeLists.get(i).getTag() == null)
//            strings.add("");
//        else {
        if (!"".equals(noticeLists.get(i).getTag()) && noticeLists.get(i).getTag() != null) {
            if (noticeLists.get(i).getTag().contains(",")) {
                String[] s = noticeLists.get(i).getTag().split(",");
                for (int k = 0; k < s.length; k++) {
                    strings.add(s[k]);
                }
            } else {
                strings.add(noticeLists.get(i).getTag());
            }
        }

        holder.tagItems.setAdapter(new TagAdapter<String>(strings) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_characteristic,
                        holder.tagItems, false);
                TextView tvCharacteristic = view.findViewById(R.id.tvCharacteristic);
                tvCharacteristic.setText(s);
                if (position % 4 == 0)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpink_round);
                else if (position % 4 == 1)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightpurple_round);
                else if (position % 4 == 2)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightorange_round);
                else if (position % 4 == 3)
                    tvCharacteristic.setBackgroundResource(R.drawable.bg_lightgreen_round);
                return view;
            }
        });
    }

    private class ViewHolder {
        CircleImageView ivPublish;
        TagFlowLayout tagItems;
        ImageView ivDanbao;
        TextView tvPublisher, tvPublishTime, tvCheckPublisher, tvJobTitle, tvDingwei,
                tvTime, tvPerson, tvSex, tvHeight, tvBaoxiao, tvFuzhuang, tvEntered, tvPrice;
    }
}
